//
//  JiaZhenViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JiaZhenViewController.h"

#import "GXCell.h"
#import "JiaZDetViewController.h"
#import "JZSearchViewController.h"
@interface JiaZhenViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSInteger pageNo;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation JiaZhenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self loadNav];
    array = [NSMutableArray array];
    pageNo = 1;
    [self loadUI];
    [self loadRefresh];
    
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"家政";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(0);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
    
    UIButton *btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSearch.frame = CGRectMake(ScreenWidth-40, 24, 40, 40);
    [btnSearch setImage:[UIImage imageNamed:@"搜索-(1)"] forState:UIControlStateNormal];
    [btnSearch addTarget:self action:@selector(clickSearch) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnSearch];
    
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}
//搜索
- (void)clickSearch{
    JZSearchViewController *search = [[JZSearchViewController alloc] init];
    search.lat = _lat;
    search.lng = _lng;
    [self.navigationController pushViewController:search animated:YES];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    GXCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GXCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array[indexPath.row];
    NSString *figurepic = [dic objectForKey:@"figurepic"];
    NSArray *arr = [figurepic componentsSeparatedByString:@"|"];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],arr[0]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM截图20170819150938"]];
    cell.labTit.text = [dic objectForKey:@"name"];
    cell.labDet.text = [dic objectForKey:@"profile"];
    
    NSString *km = [NSString stringWithFormat:@" %.2fkm",[[dic objectForKey:@"km"] floatValue]];
    NSString *order = [NSString stringWithFormat:@"%li ",[[dic objectForKey:@"order"] integerValue]];
    NSString *count = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"countcomment"] integerValue]];
    
    [cell.btnL setTitle:km forState:UIControlStateNormal];
    [cell.btnC setTitle:order forState:UIControlStateNormal];
    [cell.btnR setTitle:count forState:UIControlStateNormal];
    
    NSString *star = [dic objectForKey:@"star"];
    CGFloat counts = [star floatValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    starView.showStar = counts * 20;
    [cell.starView addSubview:starView];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 154.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JiaZDetViewController *det = [[JiaZDetViewController alloc] init];
    NSDictionary *dic = array[indexPath.row];
    det.merid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    det.name = [dic objectForKey:@"name"];
    det.det = [dic objectForKey:@"profile"];
    det.place = [dic objectForKey:@"suite"];
    det.order = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"order"] integerValue]];
    det.star = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"star"] integerValue]];
    NSString *figurepic = [dic objectForKey:@"figurepic"];
    NSArray *arr = [figurepic componentsSeparatedByString:@"|"];
    NSMutableArray *datas = [NSMutableArray array];
    for (NSString *str in arr) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],str];
        [datas addObject:imgUrl];
    }
    det.images = datas;
    
    [self.navigationController pushViewController:det animated:YES];
}
//刷新控件
- (void)loadRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //
    [self.tableView.mj_header beginRefreshing];
    //根据位置设置半透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}
//家政
- (void)loadMoreData{
    [_tableView.mj_header endRefreshing];
    pageNo++;
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lat forKey:@"longitude"];
    [parmas setObject:_lng forKey:@"latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%li",pageNo] forKey:@"p"];
    NSLog(@"parmas%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/merchant",urlHead];
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政res:%@",responseObject);
        NSArray *arr =[responseObject objectForKey:@"result"];
        if (arr.count > 0) {
            [array addObjectsFromArray:arr];
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
}

//家政列表
- (void)loadData{
//    [SVProgressHUD show];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [_tableView.mj_footer endRefreshing];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:@"1" forKey:@"p"];
    NSLog(@"parmas%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/merchant",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政res:%@",responseObject);
        
        array = [responseObject objectForKey:@"result"];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
